package org.example.springtransdemo.controller;

import org.example.springtransdemo.entity.UserInfo;
import org.example.springtransdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;
    @RequestMapping("/registry")
    public String registry(UserInfo userInfo) {
        //开启事务
        TransactionStatus transactionStatus=dataSourceTransactionManager.getTransaction(transactionDefinition);
        //用户注册
        userService.insertUser(userInfo);
        //提交/回滚事务
//        dataSourceTransactionManager.rollback(transactionStatus);
        dataSourceTransactionManager.commit(transactionStatus);
        return "注册成功！";
    }
}
